//
//  LTFReadBookMarkView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/22.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFReadBookMarkView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFReadBookMarkLeftModel.h"
#import "LTFReadLeftTableViewCell.h"
#import "LTFReadBookMarkRightModel.h"
#import "LTFReadRightTableViewCell.h"
#import "LTFReadMarkRightModel.h"
#import "GTMBase64.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
@interface LTFReadBookMarkView ()
<
 UISearchBarDelegate,
 UITableViewDataSource,
 UITableViewDelegate
>
@property (nonatomic, retain) UISearchBar *search;
@property (nonatomic, retain) UITableView *leftTableView;
@property (nonatomic, retain) NSMutableArray *leftArray;
@property (nonatomic, retain) UITableView *rightTableView;
@property (nonatomic, retain) NSMutableArray *rightArray;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSMutableArray *searchArray;
@property (nonatomic, retain) UITableView *searchTableView;
@property (nonatomic, retain) NSString *value;
@property (nonatomic, assign) NSInteger index;
@end

@implementation LTFReadBookMarkView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        [self createData];
        [self createAnotherData];
    }
    return self;
}


- (void)createView {
    
    //搜索
    self.search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    _search.backgroundColor = [UIColor redColor];
    [self addSubview:_search];
    [_search release];
    self.search.delegate = self;
    self.search.placeholder = @"搜索";
    
    
    self.searchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT - 40) style:UITableViewStylePlain];
    _searchTableView.rowHeight = (HEIGHT - 40) / 8.5;
    _searchTableView.delegate = self;
    _searchTableView.dataSource = self;
    _searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.searchTableView registerClass:[LTFReadRightTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFReadRightTableViewCell class])];
    
    //self.search.keyboardType = UIKeyboardTypeDefault;
    //左侧
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, WIDTH / 4 , (HEIGHT - 40 - 64 - 48)) style:UITableViewStylePlain];
    [self addSubview:self.leftTableView];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.rowHeight = (HEIGHT - 40) / 8.5;
    [self.leftTableView registerClass:[LTFReadLeftTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFReadLeftTableViewCell class])];
    self.leftTableView.backgroundColor = [UIColor redColor];
    self.leftTableView.tag = 1004;
    
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //右侧
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 4, 40, WIDTH / 4 * 3, (HEIGHT - 40 - 64 - 48)) style:UITableViewStylePlain];
    [self addSubview:self.rightTableView];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.rowHeight = (HEIGHT - 40) / 8.5;
    self.rightTableView.tag = 1005;
    [self.rightTableView registerClass:[LTFReadRightTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFReadRightTableViewCell class])];
    
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.search resignFirstResponder];
    self.searchArray = [NSMutableArray array];
    NSString *string = @"http://c.m.163.com/nc/topicset/v5/android/search.html";
    self.value = searchBar.text;
    NSString *encodStr = [GTMBase64 encodeBase64String:_value];
    NSDictionary *dic = @{@"keyWord": encodStr};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager POST:string parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSLog(@"%@", dic);
        NSArray *result = dic[@"result"];
        self.searchArray = [LTFReadBookMarkRightModel BaseModelByArray:result];
        [self addSubview:_searchTableView];
        [self.searchTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.leftTableView) {
        return self.leftArray.count;
    }
    else if (tableView == self.rightTableView){
    
        return self.rightArray.count;
    }
    else {
        return self.searchArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        LTFReadBookMarkLeftModel *model = self.leftArray[indexPath.row];
         LTFReadLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFReadLeftTableViewCell class]) forIndexPath:indexPath];
        if (indexPath.row == self.index) {
            cell.myLabel.textColor = [UIColor redColor];
        }
        else {
            cell.myLabel.textColor = [UIColor grayColor];
        }
        cell.myLabel.text = model.cName;
        return cell;
    }
    else if (tableView == self.rightTableView){
        LTFReadRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFReadRightTableViewCell class]) forIndexPath:indexPath];
        LTFReadBookMarkRightModel *model = self.rightArray[indexPath.row];
        cell.model.title = model.tname;
        cell.model.pic = [NSString stringWithFormat:@"http://s.cimg.163.com/pi/img3.cache.netease.com/m/newsapp/topic_icons/%@.png.100x100.75.auto.webp", model.img];
        cell.model.subscribe = [NSString stringWithFormat:@"%@订阅",model.subnum];
        return cell;
    }
    else {
        LTFReadRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFReadRightTableViewCell class]) forIndexPath:indexPath];
        LTFReadBookMarkRightModel *model = self.searchArray[indexPath.row];
        cell.model.title = model.tname ;
        cell.model.pic = [NSString stringWithFormat:@"http://s.cimg.163.com/pi/img3.cache.netease.com/m/newsapp/topic_icons/%@.png.100x100.75.auto.webp", model.img];
        return cell;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        LTFReadBookMarkLeftModel *model = self.leftArray[indexPath.row];
        self.address = model.cid;
        [self createAnotherData];
//        LTFReadLeftTableViewCell *cell = (LTFReadLeftTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//        cell.myLabel.backgroundColor = [UIColor redColor];
//        LTFReadLeftTableViewCell *oldCell = (LTFReadLeftTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//        oldCell.myLabel.backgroundColor = [UIColor whiteColor];
        self.index = indexPath.row;
        
        [self.leftTableView reloadData];
        
    }
    if (tableView == self.rightTableView) {
        LTFReadBookMarkRightModel *model = self.rightArray[indexPath.row];
        [self.dalegate sendAddress:model.tid title:model.tname];
    }
    if (tableView == self.searchTableView) {
        
        LTFReadBookMarkRightModel *model = self.searchArray[indexPath.row];
        [self.dalegate sendAddress:model.tid title:model.tname];
    }
}


- (void)createAnotherData {
    self.rightArray = [NSMutableArray array];
    NSString *string = [NSString string];
    if (self.address == nil) {
        string = @"http://c.m.163.com/nc/topicset/v5/ios/C1378977951794/read/all.html";
    }
    else {
        string = [NSString stringWithFormat:@"http://c.m.163.com/nc/topicset/v5/ios/%@/read/all.html", self.address];
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSArray *tList = dictionary[@"tList"];
        self.rightArray = [LTFReadBookMarkRightModel BaseModelByArray:tList];
        [self.rightTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}


//解析地址
- (void)createData {
    self.leftArray = [NSMutableArray array];
    
    NSString *string = @"http://c.m.163.com/nc/topicset/v5/ios/home.html";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSArray *iosCategoryList = dictionary[@"iosCategoryList"];
        self.leftArray = [LTFReadBookMarkLeftModel BaseModelByArray:iosCategoryList];
        NSRange range = {0, 2};
        [self.leftArray removeObjectsInRange:range];
        [self.leftTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

- (void)dealloc {
    [_search release];
    [_rightTableView release];
    [super dealloc];
}
@end
